package com.byxy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.byxy.entity.Forum;
import com.byxy.entity.Tab;
import com.byxy.service.ForumService;
import com.byxy.service.TabService;

@Controller
public class TabController {
	@Autowired
	private TabService tabService;

	@Autowired
	private ForumService forumService;

	@RequestMapping("toTabManagePage.do")
	public String findAll(Model model) {
		List<Tab> tabs = tabService.findAll();
		for (Tab tab : tabs) {
			Forum f = forumService.get(tab.getForum().getForumId());
			tab.getForum().setForumName(f.getForumName());
		}
		model.addAttribute("tabs", tabs);
		return "tabManage";
	}

	@GetMapping("toAddTabPage.do")
	public String toAddTabPage(Model model) {
		model.addAttribute("forums", forumService.findAll());
		return "addTab";
	}

	@RequestMapping("addTab.do")
	public String addTab(@RequestParam("tab_name") String tabName, @RequestParam("selectForum") Integer forumId) {
		tabService.add(tabName, forumId);
		return "redirect:toTabManagePage.do";
	}

	@RequestMapping("toModifyTabPage.do")
	public String toModifyTabPage(@RequestParam("tabId") Integer tabId, Model model) {
		model.addAttribute("forums", forumService.findAll());
		model.addAttribute("tab", tabService.get(tabId));
		return "modifyTab";
	}

	@RequestMapping("modifyTabInfo.do")
	public String modifyTabInfo(Model model, @RequestParam("tab_id") Integer tabId,
			@RequestParam("tab_name") String tabName, @RequestParam("selectedForumId") Integer selectedForumId) {
		model.addAttribute("tab", tabService.get(tabId));
		tabService.update(tabName, selectedForumId, tabId);
		return "redirect:toTabManagePage.do";
	}

	@RequestMapping("disEnableTab.do")
	public String disEnableTab(@RequestParam("tabId") Integer tabId) {
		tabService.delete(tabId);
		return "redirect:toTabManagePage.do";
	}

}
